package com.sykuku.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sykuku.common.R;
import com.sykuku.controller.expection.BusinessException;
import com.sykuku.dto.SetmealDto;
import com.sykuku.pojo.*;
import com.sykuku.service.CategoryService;
import com.sykuku.service.DishService;
import com.sykuku.service.SetmealDishService;
import com.sykuku.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;
    //套餐分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {

        log.info("用户套餐管理,起始页码 {},每页显示条数{},查询条件{}", page, pageSize,name);
        //按条件先查询出套餐并分页的信息
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //一定判断非空不然会把空当做条件
        lqw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(setmealPage, lqw);

        //返回值中需要分类名称需要set进去
        ArrayList<SetmealDto> list = new ArrayList<>();

        Page<SetmealDto> setmealDtoPage = new Page<>();

        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        records.stream().forEach((setmeal) ->{
            //对象拷贝
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            list.add(setmealDto);
        });
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }
    //新增套餐
    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);

        setmealService.setmealWithDish(setmealDto);

        return R.success("新增套餐成功");
    }
    //回显套餐分类
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }


    //批量启停售
    @PostMapping("/status/{status}")
    public R<String> update(@RequestParam("ids") List<Long> ids, @PathVariable Integer status){
        log.info("起停售{},启停售的id{}",status,ids);
        //遍历dis获取对象后设置status状态 并整个对象根据id更新数据
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("状态修改成功");
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("用户对套餐进行删除操作,ids为:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> RetUpdate(@PathVariable Long id){
        //查询setmeal订单所信息
        Setmeal setmeal=setmealService.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        //把setmeal信息拷贝dto另外查询菜品信息
        BeanUtils.copyProperties(setmeal, setmealDto);

        //查询当前套餐对应的菜品信息，从setmealDish表查询
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        //setmeal中包含的所有菜品信息放入dto返回
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);



    }


}
